#!/bin/bash
#卸载系统自带的mysql数据库
rpm -e --nodeps $(rpm -qa | grep mysql)
rpm -e --nodeps $(rpm -qa | grep mariadb)

#创建mysql组和mysql用户
groupadd mysql
useradd -r -g mysql mysql

#安装mysql，解压
tar xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql

#创建data目录
mkdir /usr/local/mysql/data

#将/usr/local/mysql的属组和属主改为mysql
chown -R mysql.mysql /usr/local/mysql

#在/usr/local/mysql/support-files/目录下创建my_default.ccnf
cat >> /usr/local/mysql/support-files/my_default.cnf << EOC
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8

log-error = /usr/local/mysql/data/mysqld.log
pid-file = /usr/local/mysql/data/mysqld.pid
EOC

#把/usr/local/mysql/support-files复制到/etc/my.cnf
cp /usr/local/mysql/support-files/my_default.cnf /etc/my.cnf

#初始化mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

#提取密码
tail -1 /usr/local/mysql/data/mysqld.log | awk '{print $11}' > dba.passwd

#复制启动项
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

#mysq优化
ln -s /usr/local/mysql/bin/* /usr/local/sbin/
ln -s /usr/local/mysql/bin/* /usr/local/bin/

#启动mysql
service mysqld start

#改密码
/usr/local/mysql/bin/mysqladmin -uroot -p$(cat dba.passwd) password 666666
/usr/local/mysql/bin/mysql -uroot -p666666 -e "grant all privileges on *.* to root@'%' identified by '123456';"
